package com.hiep.connect.client.view;

import java.util.Date;
import java.util.List;

import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.hiep.connect.shared.Posting;
import com.hiep.connect.shared.PostingThread;

public class ThreadPanel extends DecoratorPanel {
	private String email;

	public String getEmail() {
		return email;
	}

	public PostingThread getThread() {
		return thread;
	}

	private PostingThread thread;

	public ThreadPanel(String email, PostingThread thread) {
		this.email = email;
		this.thread = thread;
		initGui();
	}

	private void initGui() {
		Date updateDate = thread.getUpdateDate();
		List<Posting> replies = thread.getReplies();

		// TODO first original posting
		// SimplePanel firstPost = new SimplePanel();

		VerticalPanel outerPanel = new VerticalPanel();
		outerPanel.setSpacing(5);
		for (int i = 0; i < replies.size(); i++) {
			// VerticalPanel aPanel = new VerticalPanel();
			String friendEmail = replies.get(i).getEmail();
			String content = replies.get(i).getContent();
			Date date = replies.get(i).getDate();
			// if (content != null) {
			// aPanel.add(new Label(content));
			// }
			// if (date != null) {
			// aPanel.add(new Label(date.toString()));
			// }

			FlexTable table = new FlexTable();
			table.setCellSpacing(2);

			Image image = new Image("images/person.png");
			HorizontalPanel itemPanel = new HorizontalPanel();
			itemPanel.add(image);
			itemPanel.add(new Label(friendEmail));

			table.setWidget(0, 0, itemPanel);
			if (date != null) {
				table.setWidget(0, 1, new Label(date.toString()));
			}
			table.setWidget(1, 1, new Label(content));

			outerPanel.add(table);
		}

		this.add(outerPanel);
	}

	public Widget asWidget() {
		return this;
	}
}
